// pages/Editor/EditorImgs/editorimgs.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: null,
    imgList: [],
    base:"",
    id:"",
    getdata:""

  },

   //上传图片
   ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  ChooseImage() {
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
  //删除
  DelImg(e) {
    wx.showModal({
      title: '召唤师',
      content: '确定要删除这段回忆吗？',
      cancelText: '再看看',
      confirmText: '再见',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
//修改图片改成base64


exitor:function(){
  let that=this;
 // console.log(that.data.imgList[0])
 
  wx.request({
    url:that.data.imgList[0],
    responseType: 'arraybuffer', //最关键的参数，设置返回的数据格式为arraybuffer
    success:res=>{
      //把arraybuffer转成base64
          let base64 = wx.arrayBufferToBase64(res.data); 
          
          //不加上这串字符，在页面无法显示的哦
          base64　= 'data:image/jpeg;base64,' + base64　
          
          wx.request({
            url: 'http://localhost:2238/api/Authentication/exitorimg',//获取模特信息 , id是modelid
            method:'Post',
            data:{id:this.data.id,img:base64},
            header: {'content-type':'application/x-www-form-urlencoded' }, 
            success: function (res) {
              console.log(res.data)
                if(res.data.Code==200)
                {
                  wx.navigateTo({
                    url: '/pages/MyDatum/index?id='
                  })
                }
            }
          })

        }
  })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.request({
      url: 'http://localhost:2238/api/ModelLibrary/GetPhotoAlbum?userId=321FCFA7-79AF-420A-B612-4068CFF30857&aduit=1',
      method:'get',
      header: {'content-type':'application/x-www-form-urlencoded' }, 
      success:  (res)=> {
        let i=0;
        var that = this;
        res.data.Data.forEach(function(itme,Index){
            var param = {};
            var string = "imgList[" + Index + "]";
            param[string] = itme.Photos;
            that.setData(param);
        })
        console.log(that)
      }
    })

    console.log(this.data.imgList)
  },
  //修改个人相册
  show:function(){
    console.log(11)
    let that=this;
    var param = [];
        let promis=new Promise(function(resolve, reject){
          for(let i=0;i<that.data.imgList.length;i++)
          {
                //转成base64
                if(that.data.imgList[i].length<900)
                {
                  wx.request({
                    url:that.data.imgList[i],
                    responseType: 'arraybuffer', //最关键的参数，设置返回的数据格式为arraybuffer
                    success:res=>{
                      //把arraybuffer转成base64
                          let base64 = wx.arrayBufferToBase64(res.data); 
                          //不加上这串字符，在页面无法显示的哦
                          base64　= 'data:image/jpeg;base64,' + base64　
                          param[i] = base64　;
                          if(that.data.imgList.length==param.length)
                              resolve(param);
                        }
                  })
                }else
                {
                  param[i]=that.data.imgList[i];
                }
              }
              resolve(param);
        });
           
           
      promis.then(function(value ){
        console.log(value);
        //修改相册
        wx.request({
          url: 'http://localhost:2238/api/ModelLibrary/UpdateMyImgs',
          method:'post',
          data:{user:'321FCFA7-79AF-420A-B612-4068CFF30857',img:JSON.stringify(value)},
          header: {'content-type':'application/x-www-form-urlencoded' }, 
          success:  (res)=> {
            console.log(that)
          }
        })


          });
            
         
  },
  exitor(url){
    wx.request({
      url:url,
      responseType: 'arraybuffer', //最关键的参数，设置返回的数据格式为arraybuffer
      success:res=>{
	      //把arraybuffer转成base64
            let base64 = wx.arrayBufferToBase64(res.data); 
            
            //不加上这串字符，在页面无法显示的哦
            base64　= 'data:image/jpeg;base64,' + base64　
            
            //打印出base64字符串，可复制到网页校验一下是否是你选择的原图片呢
            console.log(base64)　
          }
    })
    console.log(this.data.imgList)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})